package com.jfinal.club._admin.auth;

import com.jfinal.club.common.model.Account;

/**
 * 权限管理的 shared method 扩展
 *
 * 作为 #role、#permission 指令的补充，支持 #else 块
 *
 *
 * 使用示例：
 * #if (hasRole("权限管理员", "CEO", "CTO"))
 *   ...
 * #else
 *   ...
 * #end
 *
 * #if (hasPermission("/admin/project/edit"))
 *   ...
 * #else
 *   ...
 * #end
 */
public class AdminAuthKit {

	/**
	 * 当前账号是否拥有某些角色
	 */
	public boolean hasRole(String... roleNameArray) {
		Account account = AdminAuthInterceptor.getThreadLocalAccount();
		if (account != null && account.isStatusOk()) {
			if (	AdminAuthService.me.isSuperAdmin(account.getId()) ||
					AdminAuthService.me.hasRole(account.getId(), roleNameArray)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 是否拥有具体某个权限
	 */
	public boolean hasPermission(String actionKey) {
		Account account = AdminAuthInterceptor.getThreadLocalAccount();
		if (account != null && account.isStatusOk()) {
			if (	AdminAuthService.me.isSuperAdmin(account.getId()) ||
					AdminAuthService.me.hasPermission(account.getId(), actionKey)) {
				return true;
			}
		}

		return false;
	}
}


